package com.miaostars.apakey.utils;

import com.miaostars.apakey.Apakey;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class AlertManager {
    
    private final Apakey plugin;
    private final Set<UUID> alertReceivers;
    
    public AlertManager(Apakey plugin) {
        this.plugin = plugin;
        this.alertReceivers = new HashSet<>();
    }
    
    public void alert(Player player, String check, String type) {
        if (!plugin.getConfigManager().getConfig().getBoolean("alerts.enabled")) {
            return;
        }
        
        String prefix = ChatColor.translateAlternateColorCodes('&', 
            plugin.getConfigManager().getConfig().getString("alerts.prefix"));
        
        String message = ChatColor.translateAlternateColorCodes('&', 
            plugin.getConfigManager().getConfig().getString("alerts.message")
                .replace("%player%", player.getName())
                .replace("%check%", check)
                .replace("%type%", type));
        
        for (UUID uuid : alertReceivers) {
            CommandSender receiver = Bukkit.getPlayer(uuid);
            if (receiver != null) {
                receiver.sendMessage(prefix + " " + message);
            }
        }
        
        // Log to console
        plugin.getLogger().info(ChatColor.stripColor(prefix + " " + message));
    }
    
    public void toggleAlerts(CommandSender sender) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(ChatColor.RED + "Only players can toggle alerts!");
            return;
        }
        
        Player player = (Player) sender;
        UUID uuid = player.getUniqueId();
        
        if (alertReceivers.contains(uuid)) {
            alertReceivers.remove(uuid);
            player.sendMessage(ChatColor.RED + "Alerts disabled!");
        } else {
            alertReceivers.add(uuid);
            player.sendMessage(ChatColor.GREEN + "Alerts enabled!");
        }
    }
}